<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{ 
	/**
	 * @var string the default layout for the controller view. Defaults to 'application.views.layouts.column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='application.views.layouts.column1';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
	
	public $pageDescription = 'АВТОДИНАМИКА предлагает вашему вниманию широкий спектр услуг по ремонту и диагностике автомобилей.';
	public $pageKeyWords = 'автозапчасти,магазин автозапчастей,автозапчасти для иномарок,автозапчасти москва,продажа автозапчастей, автозапчасти интернет,осаго,автострахование,техцентр bmw,ремонт вмятин,полировка фар';
	
	
	/**
	 * Фильтры
	 */
	public function filters(){
		
		return array(
			'shoppingCartWorkout',
			'selectionFormValidation',
		);
	}
	
	// корзинка
	function filterShoppingCartWorkout($filterChain){
		if(isset($_POST['ajax']) && strpos($_POST['ajax'], 'buy-form') === 0 ) {
			$model = new BasketForm;
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		
		if(isset($_GET['ajax']) && $_GET['ajax'] === 'basket' && $_GET['operation'] === 'clear') {
			Yii::app()->shoppingCart->clear();
		}
		
		if(isset($_POST['BasketForm'])) {
			$model = new BasketForm;
			$model->attributes = $_POST['BasketForm'];
			if($model->validate()) {
				list($class, $id) = preg_split('/_/', $model->id);
				$cart = Yii::app()->shoppingCart;
				$class = new $class;
				$cart->put($class->findByPk($id),$model->quantity);
			} else {
				echo CActiveForm::validate($model);
				Yii::app()->end();
			}
		}
		
		$filterChain->run();
	}
	
	function filterSelectionFormValidation($filterChain){
		
		$tyreSelectionForm = Yii::app()->request->getParam('TyreSelectionForm', array());
		$tyreSelection = new TyreSelectionForm();
		if(!empty($tyreSelectionForm)) {
			if(isset($_REQUEST['ajax']) && $_REQUEST['ajax']==='tyreSelectionForm') {
				echo CActiveForm::validate($tyreSelection);
				Yii::app()->end();
			}
		}

		$diskSelectionForm = Yii::app()->request->getParam('DiskSelectionForm', array());
		$diskSelection = new DiskSelectionForm();
		if(!empty($diskSelectionForm)) {
			if(isset($_REQUEST['ajax']) && $_REQUEST['ajax']==='diskSelectionForm') {
				echo CActiveForm::validate($diskSelection);
				Yii::app()->end();
			}
		}
		
		$filterChain->run();
	}
}